/**
 * All rights Reserved, Designed By www.tydic.com
 * @Title: EasyPaySignUtil.java
 * @Package: com.example.down
 * @Description:
 * @author: wangtao
 * @date: 2019年10月15日 下午3:06:54
 */
package com.hlkj.pay.app.merchant.ext.impl.icbcpay.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.ext.impl.icbcpay.dto.req.HttpRequest;

import java.util.*;

/**
 * @ClassName: GhPaySignUtil
 * @Description:
 * @author: wangtao
 * @date: 2019年10月15日 下午3:06:54
 */
public class IcbcPaySignUtil {

	/**
	 * 使用商户的私钥进行加签
	 *
	 * @param request: 签名数据，注意这里仅仅是文档中data字段的内容
	 * @return java.lang.String 私钥key
	 */
	public static String sign(HttpRequest request, String privateKey) throws Exception {
		SortedMap<String,Object> reqMap = JSONObject.parseObject(JsonUtils.toJsonString(request), new TypeReference<TreeMap<String,Object>>(){});
		String signContent = createSignStr(reqMap);
		return IcbcPayRsaUtil.signBySHA256WithRSA(signContent, privateKey);
	}

	/**
	 * @param parameters 数据对象 不能都为空
	 * @return 签名排序字符串
	 */
	public static String createSignStr(SortedMap<String, Object> parameters) {
		StringBuilder sb = new StringBuilder();
		Set<Map.Entry<String,Object>> es = parameters.entrySet();
		for (Map.Entry<String,Object> e : es) {
			String k =  e.getKey();
			String v = (e.getValue() == null ||
					e.getValue() instanceof List ||
					e.getValue().getClass().isArray()
			) ? "" : e.getValue().toString();
			//空值不传递，不参与签名组串
			if (null != v && !"".equals(v) && !"signature".equals(k) && !"sign".equals(k)) {
				sb.append(k).append("=").append(v).append("&");
			}
		}
		return sb.substring(0,sb.length() -1);
	}

	public static boolean verify(String content, String publicKey) {
		SortedMap<String,Object> parameters = JSONObject.parseObject(content, new TypeReference<TreeMap<String,Object>>(){});
		String sign = parameters.get("signature").toString();
		String signContent = createSignStr(parameters);
		return IcbcPayRsaUtil.verifyBySHA256WithRSA(signContent, sign, publicKey);
	}

	public static void main(String[] args) {
		String res = "{\"code\":\"00000000\",\"merchantNo\":\"XSSY20250314000009\",\"message\":\"成功\",\"outTradeNo\":\"D20250321110029605822464\",\"payBrand\":\"ALIPAY\",\"payFinishTime\":\"20250321110105\",\"signature\":\"jfImZ1\\/1KfpX0RP9I5XqVsrGwZ6fB6uu+8Gclr0NUaqsChTDwCn6RN4W1NU1LSW9zUuaNB\\/ba3b3WrStyqsIbGtIucCa0c7ABZ4Dtes769LyoNyRc2O\\/PLUY0+15wR9cmCQVokza10b5dVsMkiy1GGyx8Rt1AwYvedw6PnU6+DxJXwx7t9pzcy1GFtqch+96gNWMF4ajNrloaEOi1t8PcjmVAppf+M\\/WELkWjfsOubF1dj\\/7pQtPCMTYf\\/cuufNUBSxT8CVqoe\\/6+3kqzSBWcFVTk+K\\/Cp\\/old+DBY4aGdm\\/Xp7A0X2ffgDLQZ4I9iCm06Gecs9z4e9iiIHKaV8aNw==\",\"tradeAmount\":\"1\",\"tradeFee\":\"0\",\"tradeNo\":\"2250321000000001246\",\"tradeStatus\":\"SUCCESS\",\"tradeSubject\":\"测试商品\",\"transNonce\":\"49a8fe8dba834eacb9655ae849258542\"}";
		String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkjcqW5Yt7K77AEcNE5DBwtcns4PgwzJJGBjl0Oz+Oi/9b/sXMn+NzUiBl6KhLcma2LElXpsr2f5fK1cuUQqRHUP8ag0w0MY94HEpunwOqUUrYP76c0XYCjfiMbpAl1fmeur0HaHtSIjLD5ND/Z+szwaDcD5XJnYJNzH0W01c7915sHrslvukp3fhsbqGIymERiov859kxNQ3wgYCaatuDZUg3/ydBtRGshz/n5tmrmYViDexAANC8EZwl4XVEXajk6zs17dqFLOxW4i0+1QpOBwXXPZSfbGW0c9dO8rPykmsmzWRUvDEuENMYfV1+opLW1zHL3f8+bdDrbHbrvdAYwIDAQAB";
		System.out.println(verify(res, publicKey));
	}
}
